<template>
  <div class="chart-wrapper" ref="chartRef"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'DailyCharts',
  props: {
    xData: {
      type: Array,
      required: true
    },
    planData: {
      type: Array,
      required: true
    },
    actualData: {
      type: Array,
      required: true
    }
  },
  mounted() {
    this.initChart();
  },
  watch: {
    xData: 'initChart',
    planData: 'initChart',
    actualData: 'initChart'
  },
  methods: {
    initChart() {
      if (!this.$refs.chartRef) return;
      if (this.chart) this.chart.dispose();
      this.chart = echarts.init(this.$refs.chartRef);

      this.chart.setOption({
        backgroundColor: 'transparent',
        grid: {
          left: 0,
          right: 0,
          top: 40,
          bottom: 20,
          containLabel: true
        },
        title: {
          text: '周产量(个)',
          left: 0,
          top: 0,
          textStyle: {
            color: '#888ca0',
            fontSize: 14,
            fontWeight: 'normal'
          }
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['计划', '实际'],
          right: 10,
          top: 0,
          itemWidth: 10,
          itemHeight: 10,
          textStyle: {
            color: '#888ca0',
            fontSize: 14
          }
        },
        xAxis: {
          type: 'category',
          data: this.xData,
          axisLine: { lineStyle: { color: '#888ca0' } },
          axisLabel: { color: '#888ca0', fontSize: 14 },
          axisTick: { show: false }
        },
        yAxis: {
          type: 'value',
          min: 0,
          interval: 20,
          axisLine: { show: false },
          axisLabel: { color: '#888ca0', fontSize: 14 },
          splitLine: { lineStyle: { color: '#333' } }
        },
        series: [
          {
            name: '计划',
            type: 'line',
            data: this.planData,
            smooth: true,
            symbol: 'circle',
            symbolSize: 10,
            lineStyle: { color: '#2de3f7', width: 2 },
            itemStyle: { color: '#2de3f7' }
          },
          {
            name: '实际',
            type: 'line',
            data: this.actualData,
            smooth: true,
            symbol: 'circle',
            symbolSize: 10,
            lineStyle: { color: '#ffb21a', width: 2 },
            itemStyle: { color: '#ffb21a' }
          }
        ]
      });
    }
  },
  beforeDestroy() {
    if (this.chart) this.chart.dispose();
  }
};
</script>

<style scoped>
.chart-wrapper {
  width: 100%;
  height: 220px;
  background: transparent;
}
</style>
